﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace FirstGame
{
    public class ButtonItem : MenuItem
    {

        public ButtonItem(ContentManager content,MenuScreen parent, string strTexture1, string strTexture2, Vector2 topleft, Vector2 inittopleft, Vector2 size)
        {
            TopLeft = topleft;
            _InitTopleft = inittopleft;
            _CurrentTopleft = _InitTopleft;
            Size = size;
            topleft = InitMenuItem(content, parent, strTexture1, strTexture2, topleft, inittopleft, size);
            
            
        }

        /// <summary>
        /// create a meuitem from xmlnode
        /// </summary>
        /// <param name="content"></param>
        /// <param name="node"></param>
        public ButtonItem(ContentManager content, MenuScreen parent, XmlNode node)
        {
            float x = float.Parse(node.Attributes["x"].Value);
            float y = float.Parse(node.Attributes["y"].Value);
            float with = float.Parse(node.Attributes["with"].Value);
            float height = float.Parse(node.Attributes["height"].Value);
            TopLeft = new Vector2(x, y);
            Size = new Vector2(with, height);
            string texture1 = node.Attributes["texture1"].Value;
            string texture2 = node.Attributes["texture2"].Value;
            // tao 2 sprite cua MyButton
            //InitMenuItem(content, parent, texture1, texture2, TopLeft, inittopleft);
            
        }


        private Vector2 InitMenuItem(ContentManager content, MenuScreen parent, string strTexture1, string strTexture2, Vector2 topleft, Vector2 inittopleft, Vector2 size)
        {

            _Menuscreen = parent;
            Texture2D[] textures1 = new Texture2D[1];
            textures1[0] = content.Load<Texture2D>(strTexture1);

            Texture2D[] textures2 = new Texture2D[1];
            textures2[0] = content.Load<Texture2D>(strTexture2);

            Sprites = new List<My2Dsprite>();
            My2Dsprite Sprite1 = new My2Dsprite(textures1, _CurrentTopleft);
            Sprite1.Size = size;
            My2Dsprite Sprite2 = new My2Dsprite(textures2, _CurrentTopleft);
            Sprite2.Size = size;

            Sprites.Add(Sprite1);
            Sprites.Add(Sprite2);

            NSprite = Sprites.Count;
            // mac dinh hình chu nhat va cham la size cua button
            this.RecColis = new Rectangle(0,0,(int)this.Size.X, (int)this.Size.Y);

            double movespeed_x = (float)(TopLeft.X - InitTopleft.X) / _MoveTime.TotalMilliseconds;
            double movespeed_y = (float)(TopLeft.Y - InitTopleft.Y) / _MoveTime.TotalMilliseconds;
            _MoveSpeed = new Vector2((float)movespeed_x, (float)movespeed_y);

            
            return topleft;
        }
       
        
        public override void Update(GameTime gameTime, InputSystem input)
        {
            if (!_IsMoving && _IsAppear)
            {
                if (this.IsClicked(input.CurPoint))
                {
                    IsFocus = true;
                    // khi button duoc focus chua hang tro chuot nam tai vi tri cua button do
                    this.Focus();
                    if (input.IsNewMouseClick(ButtonState.Pressed))
                    {
                        this.Select();
                    }
                }
                else
                {
                    IsFocus = false;
                }
            }

            if (_IsMoving)
                IsFocus = false;

            base.Update(gameTime, input);
            
        }


        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            if(IsFocus)
                Sprites[1].Draw(gameTime, spriteBatch, 180);
            else
                Sprites[0].Draw(gameTime, spriteBatch, 180);
        }

        public Vector2 SetRelativePosition()
        {
            Vector2 Temp = new Vector2();
            Temp.X = this.InitTopleft.X;
            Temp.Y = this.InitTopleft.Y + 60;
            return Temp;


        }
    }

    public abstract class MenuItem : VisibleGameEntity
    {
        // Frame ma item thuoc ve
        protected Frame _ParentFrame;

        public Frame ParentFrame
        {
            get { return _ParentFrame; }
            set { _ParentFrame = value; }
        }
        protected Rectangle _RecColis;

        public Rectangle RecColis
        {
            get { return _RecColis; }
            set { _RecColis = value; }
        }
        protected Vector2 _InitTopleft;

        public Vector2 InitTopleft
        {
            get { return _InitTopleft; }
            set { _InitTopleft = value; }
        }
        protected Vector2 _CurrentTopleft;

        public Vector2 CurrentTopleft

        {
            get { return _CurrentTopleft; }
            set { _CurrentTopleft = value; }
        }
        protected bool _IsFocus = false;

        public bool IsFocus
        {
            get { return _IsFocus; }
            set { _IsFocus = value; }
        }
        protected Vector2 _MoveSpeed = new Vector2(0, 0);

        public Vector2 MoveSpeed
        {
            get { return _MoveSpeed; }
            
        }

        protected bool _IsAppear = false;

        public bool IsAppear
        {
            get { return _IsAppear; }
            set { _IsAppear = value; }
        }

        protected bool _IsMoving = false;

        public Boolean IsMoving
        {
            get { return _IsMoving; }
            set { _IsMoving = value; }
        }

        protected TimeSpan _MoveTime = TimeSpan.FromSeconds(0.7);

        public TimeSpan MoveTime
        {
            get { return _MoveTime; }
            set 
            {
                _MoveTime = value;
                double movespeed_x = (float)(TopLeft.X - InitTopleft.X) / _MoveTime.TotalMilliseconds;
                double movespeed_y = (float)(TopLeft.Y - InitTopleft.Y) / _MoveTime.TotalMilliseconds;
                _MoveSpeed = new Vector2((float)movespeed_x, (float)movespeed_y);
            }
        }

        protected Vector2 flag = new Vector2(0, 0);


        protected MenuScreen _Menuscreen;

        public MenuScreen Menuscreen
        {
            get { return _Menuscreen; }
            set { _Menuscreen = value; }
        }

        

        protected float t = 0;
        /// <summary>
        /// event focused for item when input focus on this item
        /// 
        /// </summary>
        public event EventHandler Focused;

        public virtual void Focus()
        {
            if (Focused != null)
                Focused(this, EventArgs.Empty);
        }

        /// <summary>
        /// 
        /// </summary>
        public event EventHandler Selected;
        public virtual void Select()
        {
            if (Selected != null)
                Selected(this, EventArgs.Empty);
        }

        //khong xuat hien
        public event EventHandler Exited;
        public void Exit()
        {
            if (Exited != null)
                Exited(this, EventArgs.Empty);
        }

        // da duoc kich hoat
        public event EventHandler Entered;
        public void Enter()
        {
            if (Entered != null)
                Entered(this, EventArgs.Empty);
        }

        public override bool IsClicked(Vector2 point)
        {
            if ((point.X >= _TopLeft.X + RecColis.Left) &&
                (point.X <= _TopLeft.X + +RecColis.Left + RecColis.Width) &&
                (point.Y >= _TopLeft.Y + RecColis.Top) &&
                (point.Y <= _TopLeft.Y + +RecColis.Top + RecColis.Height))
                return true;
            return false;
            //return base.IsClicked(point);
        }

        public virtual bool MoveOnLine(GameTime gameTime, TimeSpan movetime)
        {

            float deltaX, deltaY;
            if (movetime == TimeSpan.Zero)
            {
                deltaX = (float)(TopLeft.X - InitTopleft.X);
                deltaY = (float)(TopLeft.Y - InitTopleft.Y);
            }
            else
            {
                deltaX = _MoveSpeed.X * (float)gameTime.ElapsedGameTime.TotalMilliseconds;
                deltaY = _MoveSpeed.Y * (float)gameTime.ElapsedGameTime.TotalMilliseconds;
            }
            Vector2 temp = CurrentTopleft;
            temp.X += deltaX;
            temp.Y += deltaY;

            if (_MoveSpeed.X >= 0)
            {
                if (temp.X >= TopLeft.X && flag.X != 1)
                {
                    temp.X = TopLeft.X;
                    flag.X = 1;
                }
            }
            else
            {
                if (temp.X <= TopLeft.X && flag.X != 1)
                {
                    temp.X = TopLeft.X;
                    flag.X = 1;
                }
            }

            if (_MoveSpeed.Y >= 0)
            {
                if (temp.Y >= TopLeft.Y && flag.Y != 1)
                {
                    temp.Y = TopLeft.Y;
                    flag.Y = 1;
                }
            }
            else
            {
                if (temp.Y <= TopLeft.Y && flag.Y != 1)
                {
                    temp.Y = TopLeft.Y;
                    flag.Y = 1;
                }
            }
            CurrentTopleft = temp;
            sprites[0].Position = temp;
            sprites[1].Position = temp;
            if (flag.X != 0 && flag.Y != 0)
                return false;
            return true;

        }

        public virtual void ChangeDirection()
        {
            Vector2 temp = TopLeft;
            TopLeft = InitTopleft;
            InitTopleft = temp;
            _MoveSpeed = -_MoveSpeed;
            flag = new Vector2(0, 0);
        }

        public override void Update(GameTime gameTime, InputSystem input)
        {
            if (_IsMoving)
            {
                if (!MoveOnLine(gameTime, _MoveTime))
                {
                    IsMoving = false;
                    if (IsAppear)
                    {
                        IsAppear = false;
                        Exit();
                    }
                    else
                    {
                        IsAppear = true;
                        Enter();
                    }
                }
            }

            base.Update(gameTime, input);
        }

        public virtual void Move()
        {
            ChangeDirection();
            _IsMoving = true;
        }
    }

    

    
}
